<button class="btn btn-success btn-xs" data-toggle="modal" data-target="#expenseCreate">新增</button>

<div class="modal fade" id="expenseCreate" tabindex="-1" role="dialog" aria-labelledby="expenseCreate" aria-hidden="true">
    <div class="modal-dialog modal-xs">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="font-size: 36px;">&times;</button>
                <h4 class="modal-title" id="myModalLabel">新增</h4>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">名称</label>
                    <input type="text" name="name" class="form-control" placeholder="请输入名称">
                </div>
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">签约客户</label>
                    <select name="contractedCustomerId" class="form-control">
                        <option value="">请选择客户</option>
                        @if(!empty($data))
                            @foreach($data as $key => $value)
                                <option value="{{ $key }}">{{ $value }}</option>
                            @endforeach
                        @endif
                    </select>
                </div>
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">数量</label>
                    <input type="number" name="num" class="form-control" placeholder="请输入数量">
                </div>
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">金额</label>
                    <input type="number" name="amount" class="form-control" placeholder="请输入金额">
                    <h5>余额：<span class="ajaxBalance" style="font-size:14px; color: red"></span></h5>
                    <h5>支出：<span class="ajaxExpense" style="font-size:14px; color: red"></span></h5>
                </div>
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">日期</label>
                    <input type="date" name="date" formate="" class="form-control" placeholder="请输入日期">
                </div>
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">类别</label>
                    <select name="type" class="form-control">
                        @foreach(__('finance_expenses.typeArr') as $k1 => $v1)
                            <option value="{{ $k1 }}">{{ $v1 }}</option>
                            @endforeach
                    </select>
                </div>
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">类别</label>
                    <select name="invoiceType" class="form-control">
                        @foreach(__('finance_expenses.invoiceTypeArr') as $k => $val)
                            <option value="{{ $k }}">{{ $val }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">凭证</label>
                    <input type="file" name="invoiceProof" id="fileInput" class="form-control" placeholder="请上传凭证">
                    <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                </div>
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">备注</label>
                    <textarea name="remark" class="form-control" placeholder="请输入备注"></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="incomeCreate()">提交</button>
            </div>
        </div>
    </div>
</div>
<script>

    function incomeCreate()
    {
        var formData = new FormData();
        var fileInput = $('#fileInput')[0]; // 获取文件输入元素
        var file = fileInput.files[0]; // 获取第一个文件
        formData.append('file', file); // 将文件添加到FormData对象中
        formData.append('contracted_customer_id', $("select[name='contractedCustomerId']").val());
        formData.append('name', $("input[name='name']").val());
        formData.append('num', $("input[name='num']").val());
        formData.append('amount', $("input[name='amount']").val());
        formData.append('date', $("input[name='date']").val());
        formData.append('type', $("select[name='type']").val());
        formData.append('invoice_type', $("select[name='invoiceType']").val());
        formData.append('remark', $("textarea[name='remark']").val());
        formData.append('_token', $("input[name='_token']").val());

        $.ajax({
            url: '/admin/ajax/expenseCreate', // 服务器端处理文件上传的脚本URL
            type: 'POST',
            data: formData,
            contentType: false, // 告诉jQuery不要去设置Content-Type请求头
            processData: false, // 告诉jQuery不要处理发送的数据
            success: function(response) {
                window.location.href='/admin/financeExpenses/';
            }
        });
    }

    $("select[name='contractedCustomerId']").change(function() {
        ajaxPostFunc('/admin/ajax/getContractUserAccountInfo', {
            id: $(this).val(),
            _token: $.admin.token
        }, {}, res => {
            console.log(res);
            $('.ajaxBalance').html(res.income);
            $('.ajaxExpense').html(res.expense);
            // window.location.reload();
        });
    });
</script>
